// pages/videoIndex/videoIndex.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loop: true, //视频是否循环

    // 视频列表
    VideoSwiperItem: [{
        id: 0,
        url: "http://stream4.iqilu.com/ksd/video/2020/02/17/97e3c56e283a10546f22204963086f65.mp4",
        objectFit: 'contain',
        like: 1,
        likeNum: '000',
        commentNum: '0000',
      }, {
        id: 1,
        url: "https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4",
        objectFit: 'contain',
        like: 1,
        likeNum: '111',
        commentNum: '1111',
      },
      {
        id: 2,
        url: "https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218093206z8V1JuPlpe.mp4",
        objectFit: 'contain',
        like: 0,
        likeNum: '222',
        commentNum: '2222',
      }, {
        id: 3,
        url: "https://stream7.iqilu.com/10339/article/202002/18/2fca1c77730e54c7b500573c2437003f.mp4",
        objectFit: 'contain',
        like: 1,
        likeNum: '333',
        commentNum: '3333',
      }, {
        id: 4,
        url: "https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218025702PSiVKDB5ap.mp4",
        objectFit: 'contain',
        like: 0,
        likeNum: '444',
        commentNum: '4444',
      }, {
        id: 5,
        url: "https://stream7.iqilu.com/10339/upload_transcode/202002/18/202002181038474liyNnnSzz.mp4",
        objectFit: 'contain',
        like: 1,
        likeNum: '555',
        commentNum: '5555',
      }, {
        id: 6,
        url: "https://stream7.iqilu.com/10339/article/202002/18/02319a81c80afed90d9a2b9dc47f85b9.mp4",
        objectFit: 'contain',
        like: 0,
        likeNum: '666',
        commentNum: '6666',
      }, {
        id: 7,
        url: "http://stream4.iqilu.com/ksd/video/2020/02/17/c5e02420426d58521a8783e754e9f4e6.mp4",
        objectFit: 'contain',
        like: 0,
        likeNum: '777',
        commentNum: '7777',
      }, {
        id: 8,
        url: "http://stream4.iqilu.com/ksd/video/2020/02/17/87d03387a05a0e8aa87370fb4c903133.mp4",
        objectFit: 'contain',
        like: 0,
        likeNum: '888',
        commentNum: '8888',
      }, {
        id: 9,
        url: "https://stream7.iqilu.com/10339/article/202002/17/c292033ef110de9f42d7d539fe0423cf.mp4",
        objectFit: 'contain',
        like: 1,
        likeNum: '999',
        commentNum: '9999',
      }
    ],


    pageNum: 1, //查询视频的  分页页数
    pageSize: 10, //查询视频的   分页
    videoList: [],

    index: 1, // 默认下标
    likes: '0', // 点赞数
    commentNum: 0, // 评论数
    isLike: 0, //视频是否点赞收藏  1点了  0未点
    nickName: '', //短视频发布者的昵称
    imgUrl: '', //短视频发布者的头像链接
    content: '', //content
    svId: '', //短视频id
    userId: '', //发送短视频的userId

    pageNumC: 1, //查询评论的  分页页数
    pageSizeC: 10, //查询评论的   分页
    showComment: false, //展示评论
    commentList: [], //评论列表

    value: '', //评论内容

    insertOneItem:null,//外链进入插入的一条数据
  },

  // 点击复制链接  打开陌滋
  textPaste() {
    wx.setClipboardData({
      data: app.util.config().appUrl,
      success: function (res) {
        app.success("APP链接已复制!")
        setTimeout(() => {
          app.success("前往APP体验!")
        }, 1000)
      }
    })
  },

  // 点击评论  去展示  http://52yuze.com/api/shortVideo/comment/get
  openComment() {

    if (this.check()) {

      var pageNum = this.data.pageNumC
      var pageSize = this.data.pageSizeC
      var svId = this.data.svId

      var postObj = {
        url: '/shortVideo/comment/get',
        method: 'POST',
        data: {
          userId: wx.getStorageSync('userId'),
          token: wx.getStorageSync('token'),
          svId: svId,
          pageNum: pageNum,
          pageSize: pageSize
        }
      }

      app.request.noLoginRequest(postObj).then(res => {
        console.log('点击评论res', res)
        this.setData({
          showComment: true,
          commentList: res.data.data
        })
        console.log(this.data.commentList)
      }).catch(err => {
        console.log('err', err)
      })

    }




  },
  //隐藏评论
  onCloseComment() {
    this.setData({
      showComment: false
    })
  },
  // 监听视频切换
  bindchange(e) {
    console.log(e)
    let activeId = e.detail.activeId
    console.log(activeId)
    let index = e.detail.activeId
    this.setData({
      index: index,
      isLike: this.data.videoList[index].isLike,
      likes: this.data.videoList[index].likes,
      commentNum: this.data.videoList[index].commentNum,
      nickName: this.data.videoList[index].nickName, //短视频发布者的昵称
      imgUrl: this.data.videoList[index].imgUrl, //短视频发布者的头像链接
      content: this.data.videoList[index].shortVideo.content, //content
      svId: this.data.videoList[index].shortVideo.svId, //短视频id
      userId: this.data.videoList[index].shortVideo.userId //发送短视频的userId
    })

    if (index == this.data.videoList.length - 3) {
      this.getVideoList()
    }
  },


  // 获取短视频列表
  getVideoList(data) {

    var userId = wx.getStorageSync('userId')
    var pageNum = this.data.pageNum
    var pageSize = this.data.pageSize


    var postObj = {
      url: '/shortVideo/getList',
      method: 'POST',
      data: {
        userId: wx.getStorageSync('userId') || "0",
        pageNum: pageNum,
        pageSize: pageSize
      }
    }
    app.request.noLoginRequest(postObj).then(res => {
      console.log('获取短视频列表res', res)
      // 判断页码是否要加一
      if (res.data.data.length > 0) {
        this.setData({
          pageNum: this.data.pageNum + 1
        })
      } else {
        this.setData({
          pageNum: this.data.pageNum
        })
      }
      var videoList = [...this.data.videoList, ...res.data.data]

      var insertOneItem =  this.data.insertOneItem
      console.log('insertOneItem',insertOneItem)
      if (!!insertOneItem) {
        videoList.splice(1,0,insertOneItem)
      }
      console.log('videoList', videoList)
      videoList.forEach((items, i) => {
        items.id = i
        items.url = items.shortVideo.url
        items.objectFit = "contain"
      })
      this.setData({
        videoList: videoList
      })
      let index = this.data.index
      this.setData({
        isLike: this.data.videoList[index].isLike ,
        likes: this.data.videoList[index].likes,
        commentNum: this.data.videoList[index].commentNum,
        nickName: this.data.videoList[index].nickName, //短视频发布者的昵称
        imgUrl: this.data.videoList[index].imgUrl, //短视频发布者的头像链接
        content: this.data.videoList[index].shortVideo.content, //content
        svId: this.data.videoList[index].shortVideo.svId, //短视频id
        userId: this.data.videoList[index].shortVideo.userId //发送短视频的userId
      })

    }).catch(err => {
      console.log('获取短视频列表err', err)
    })




 

  },

  // 短视频点赞接口  http://52yuze.com/api/shortVideo/like
  like() {

    if (this.check()) {
      var svId = this.data.svId
      var postObj = {
        url: '/shortVideo/like',
        method: 'POST',
        data: {
          userId: wx.getStorageSync('userId'),
          token: wx.getStorageSync('token'),
          svId: svId
        }
      }
      app.request.noLoginRequest(postObj).then(res => {
        console.log('短视频点赞接口res', res)
        let index = this.data.index
        let strisLike = `videoList[${index}].isLike`
        let strlikes = `videoList[${index}].likes`
        this.setData({
          isLike: "1",
          [strisLike]: "1",
          likes: this.data.videoList[index].likes + 1,
          [strlikes]: this.data.videoList[index].likes + 1,
        })
        app.success("点赞成功！")

      }).catch(err => {
        console.log('短视频点赞接口err', err)
       
      })
    }

  },


  // 取消点赞短视频  http://52yuze.com/api/shortVideo/unlike
  unlike() {
    if (this.check()) {
      var svId = this.data.svId
      var postObj = {
        url: '/shortVideo/unlike',
        method: 'POST',
        data: {
          userId: wx.getStorageSync('userId'),
          token: wx.getStorageSync('token'),
          svId: svId
        }
      }
      app.request.noLoginRequest(postObj).then(res => {
        console.log('取消点赞短视频res', res)
        let index = this.data.index
        let strisLike = `videoList[${index}].isLike`
        let strlikes = `videoList[${index}].likes`
        this.setData({
          isLike: "0",
          [strisLike]: "0",
          likes: this.data.videoList[index].likes - 1,
          [strlikes]: this.data.videoList[index].likes - 1,
        })
        app.success("取消点赞！")

      }).catch(err => {
        console.log('取消点赞短视频err', err)
        if (err.data.code==3002) {
          this.like()
        }
      })
    }

  },

  // 检查是否登陆
  check() {
    if (!!wx.getStorageSync('userId') && !!wx.getStorageSync('token')) {
      return true
    } else {
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }
  },

  // 评论内容change
  onChangevalue(event) {
    // event.detail 为当前输入的值
    this.setData({
      value: event.detail
    })
  },

  // 发送评论     http://52yuze.com/api/shortVideo/comment/add
  sendValue() {
    // userId	是	string	userId
    // token	是	string	验证令牌
    // toUserId	是	string	被评论的 userId
    // svId	是	int	短视频的svId
    // parentId	是	int	一级评论的commentId
    // comment	是	string	评论文字
    let index = +this.data.index
    // let parentId = this.data.commentList[index].shortVideoComment.commentId
    let commentNum = `videoList[${index}].commentNum`
    if (!!this.data.value) {
      var postObj = {
        url: '/shortVideo/comment/add',
        method: 'POST',
        data: {
          userId: wx.getStorageSync('userId'),
          token: wx.getStorageSync('token'),
          toUserId: this.data.userId,
          svId: this.data.svId,
          comment: this.data.value,
        }
      }
      app.request.noLoginRequest(postObj).then(res => {
        app.success("评论成功")
        this.setData({
          value: '',
          commentNum: +this.data.commentNum + 1,
          commentNum: +this.data.commentNum + 1,
        })
        this.openComment()
      }).catch(err => {
        app.success("评论失败")
        this.setData({
          value: ''
        })
        this.openComment()
      })
    } else {
      app.error("内容不可为空！")
    }


  },
  // 获取一条短视频接口  http://52yuze.com/api/shortVideo/getOne
  getOne(svId){
    var postObj = {
      url: '/shortVideo/getOne',
      method: 'POST',
      data: {
        svId: svId
      }
    }

    app.request.noLoginRequest(postObj).then(res=>{
      console.log('获取一条短视频接口',res)
      var data = res.data.data
      data.id = 1
      data.url = data.shortVideo.url
      data.objectFit = "contain"
      data.isLike="1" //默认点过赞吧   有报错
      this.setData({
        index:1,
        isLike: data.isLike,
        likes: data.likes,
        commentNum:data.commentNum,
        nickName: data.nickName, //短视频发布者的昵称
        imgUrl: data.imgUrl, //短视频发布者的头像链接
        content: data.shortVideo.content, //content
        svId: data.shortVideo.svId, //短视频id
        userId: data.shortVideo.userId, //发送短视频的userId
        insertOneItem:data
      })
      // console.log("封装完的数据")
      console.log('封装完的数据',this.data.insertOneItem)
      

    }).catch(err => {})



  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.setData({
      pageNum: 1, //查询视频的  分页页数
      pageSize: 10, //查询视频的   分页
      videoList: [],
    })




    // console.log("options", options)

    if (!!options.svId) {
      // this.getOne()  this.getVideoList()
      console.log("async await")
      
      async  function test(){
        that.getVideoList()
        await that.getOne(options.svId)
      }

      test()
     
    }else{
      that.getVideoList()
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!!wx.getStorageSync('token') && !!wx.getStorageSync('userId')) {
      app.imInit.imLogin()
      setTimeout(() => {
        // app.imInit.watchMessage()
      }, 1000)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.onCloseComment()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // wx.showShareMenu({
    //   withShareTicket: true,
    //   menus: ['shareAppMessage', 'shareTimeline']
    // })
    
    var svId = this.data.svId

    return {
      title: '短视频',
      desc: this.data.content,
      path: '/pages/videoIndex/videoIndex?svId=' + svId // 路径，传递参数到指定页面。
    }


  }
})